package com.ucap.bbs.controller.action;

import java.io.IOException;
import java.util.Date;

import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;
import com.ucap.bbs.pojo.UserDetail;
import com.ucap.bbs.pojo.Users;
import com.ucap.bbs.service.IUserDetailService;
import com.ucap.bbs.service.IUsersService;
import com.ucap.bbs.util.ConstantUtil;
import com.ucap.bbs.util.GUID;
import com.ucap.bbs.util.StringUtils;
import com.ucap.bbs.util.StrutsUtil;
import com.ucap.bbs.util.bean.MessageInfoBean;

public class LoginAction extends ActionSupport{
	private IUsersService usersService;
	private IUserDetailService userDetailService;
	
	public void setUserDetailService(IUserDetailService userDetailService) {
		this.userDetailService = userDetailService;
	}

	public void setUsersService(IUsersService usersService) {
		this.usersService = usersService;
	}

	public String login() throws IOException{
		if(StringUtils.isEmpty(verifyCode)){
			messageInfoBean.addMsg("请输入验证码！");
//			StrutsUtil.write(JSONObject.fromObject(msg).toString());
			StrutsUtil.write(JSONObject.fromObject(messageInfoBean).toString());
			return null;
		}
		
		String sesionCode =  (String)StrutsUtil.getSessionAttribute(VcodeAction.V_CODE);
		StrutsUtil.getSessionRemoveAttribute(VcodeAction.V_CODE);//清除session
		//判断验证码是否正确
		if(null!=sesionCode){
			if(!verifyCode.equals(sesionCode.toLowerCase())){
				messageInfoBean.addMsg("验证码错误！");
				StrutsUtil.write(JSONObject.fromObject(messageInfoBean).toString());
				return null;
			}
		}else{
				messageInfoBean.addMsg("验证码已失效，请重新登录！");
				StrutsUtil.write(JSONObject.fromObject(messageInfoBean).toString());
				return null;
		}
		
		
		if(StringUtils.isEmpty(loginName) || StringUtils.isEmpty(loginPwd)){
			messageInfoBean.addMsg("登录账号和登录密码都不能为空！");	
			StrutsUtil.write(JSONObject.fromObject(messageInfoBean).toString());
			return null;
		}
		
		Users users = this.usersService.login(loginName,loginPwd);
		if(null!=users){
			StrutsUtil.setSessionAttribute(ConstantUtil.LOGIN_USER_BEAN, users);
			//获取用户权限
			messageInfoBean.addMsg("登录成功！");
			messageInfoBean.setIsRedirect(true);
			messageInfoBean.setRedirect(StrutsUtil.getBasePath()+ "ybs_index.jsp");
			StrutsUtil.write(JSONObject.fromObject(messageInfoBean).toString());
			
			return null;
		}else{
			messageInfoBean.addMsg("登录账号或登录密码错误！");	
			StrutsUtil.write(JSONObject.fromObject(messageInfoBean).toString());
			return null;
		}
	}
	
	public String register() throws IOException{
		if(StringUtils.isEmpty(verifyCode)){
			messageInfoBean.addMsg("请输入验证码！");
//			StrutsUtil.write(JSONObject.fromObject(msg).toString());
			StrutsUtil.write(JSONObject.fromObject(messageInfoBean).toString());
			return null;
		}
		
		String sesionCode =  (String)StrutsUtil.getSessionAttribute(VcodeAction.V_CODE);
		StrutsUtil.getSessionRemoveAttribute(VcodeAction.V_CODE);//清除session
		//判断验证码是否正确
		if(null!=sesionCode){
			if(!verifyCode.equals(sesionCode.toLowerCase())){
				messageInfoBean.addMsg("验证码错误！");
				StrutsUtil.write(JSONObject.fromObject(messageInfoBean).toString());
				return null;
			}
		}else{
				messageInfoBean.addMsg("验证码已失效，请重新输入！");
				StrutsUtil.write(JSONObject.fromObject(messageInfoBean).toString());
				return null;
		}
		
		if(StringUtils.isEmpty(loginName) || StringUtils.isEmpty(loginPwd) ||  StringUtils.isEmpty(email) ){
			messageInfoBean.addMsg("登录账号，登录密码和电子邮件均不能为空！");	
			StrutsUtil.write(JSONObject.fromObject(messageInfoBean).toString());
			return null;
		}
		Users users = new Users();
		users.setLoginName(loginName);
		users.setLoginPwd(loginPwd);
		users.setEmail(email);
		users.setRegTime(new Date());
		users.setStatus("1");
		String guid = GUID.getGUID();
		users.setGuid(guid);
		
		try {
			this.usersService.insert(users);
			UserDetail userDetail = new UserDetail();
			userDetail.setUserGuid(guid);
			userDetail.setNickName(loginName);
			this.userDetailService.insert(userDetail);
			
			StrutsUtil.setSessionAttribute(ConstantUtil.LOGIN_USER_BEAN, users);
			//获取用户权限
			messageInfoBean.addMsg("注册成功！");
			messageInfoBean.setIsRedirect(true);
			messageInfoBean.setRedirect(StrutsUtil.getBasePath()+ "ybs_index.jsp");
			StrutsUtil.write(JSONObject.fromObject(messageInfoBean).toString());
		} catch (Exception e) {
			messageInfoBean.addMsg("注册失败！");
//			messageInfoBean.setIsRedirect(true);
//			messageInfoBean.setRedirect(StrutsUtil.getBasePath()+ "index.jsp");
			StrutsUtil.write(JSONObject.fromObject(messageInfoBean).toString());
//			e.printStackTrace();
		}
		
		return null;
	}
	
	private MessageInfoBean messageInfoBean = new MessageInfoBean();
	
	public MessageInfoBean getMessageInfoBean() {
		return messageInfoBean;
	}

	public void setMessageInfoBean(MessageInfoBean messageInfoBean) {
		this.messageInfoBean = messageInfoBean;
	}

	private String loginName;
	private String loginPwd;
	private String email;
	private String verifyCode;
	
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getLoginPwd() {
		return loginPwd;
	}

	public void setLoginPwd(String loginPwd) {
		this.loginPwd = loginPwd;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getVerifyCode() {
		if(!StringUtils.isEmpty(verifyCode)){
			return verifyCode.toLowerCase();
		}
		return verifyCode;
	}

	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}
}
